﻿using FastIOC.Annotation;
using FastORM.Applicaiton;
using FastORM.Model;
using FastORM.Util;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FastExecutor.Base.Design
{
    [Component]
    public class FastTemplateDB<T> : IFastBaseDB<T> where T : BaseEntity
    {
        [AutoWired]
        public DBUtil @DBUtil;
        public string TableName { set; get; }
        public string KeyName { set; get; }
        public FastTemplateDB()
        {
            TableName = MapperBuilder.GetInstance().GetMap(typeof(T)).TableName;
            KeyName = MapperBuilder.GetInstance().GetMap(typeof(T)).KeyName;
        }

        public virtual List<T> FindAll(string SQLWhereText = "", string OrderBy = "")
        {
            return @DBUtil.FindList<T>(string.Format("select * from {0} where 1=1 {1} {2}", TableName, SQLWhereText, (!string.IsNullOrEmpty(OrderBy) ? ("Order By " + OrderBy) : OrderBy)));
        }
        public virtual List<T> FindPage(string Columns, string Where, string OrderBy, int PageIndex, int PageSize)
        {
            string sql = string.Format("select {0} from {1} where  {2}", Columns, TableName, Where);
            return @DBUtil.FindListPage<T>(sql, PageSize, PageIndex, OrderBy);
        }

        public virtual int Count(string SQLWhereText = "")
        {
            return Convert.ToInt32(@DBUtil.ExecuteScar(string.Format("select count(1) from {0} where {1}", TableName, SQLWhereText)));
        }

        public virtual T FindOne(string KeyGuid)
        {
            return @DBUtil.FindOne<T>(KeyGuid);
        }

        public virtual void Insert(T Entity)
        {
            @DBUtil.Insert(Entity);
        }

        public virtual void Update(T Entity)
        {
            @DBUtil.Save(Entity);
        }

        public virtual void Update(JObject JsonEntity)
        {
            T entity = JsonConvert.DeserializeObject<T>(JsonEntity.ToString());
            List<string> UpdateFieldList = new List<string>();
            foreach (var item in JsonEntity)
            {
                UpdateFieldList.Add(item.Key);
            }
            @DBUtil.Save(entity, UpdateFieldList.ToArray());
        }

        public virtual void BatchUpdate(List<T> EntityList)
        {
            string ExceptionStr = string.Empty;
            @DBUtil.ExecuteTransition_SDB(delegate()
            {
                foreach (T Entity in EntityList)
                {
                    Update(Entity);
                }
            }, out ExceptionStr);

        }

        public virtual void BatchUpdate(JArray JsonArray)
        {
            string ExceptionStr = string.Empty;
            @DBUtil.ExecuteTransition_SDB(delegate()
            {
                foreach (JObject item in JsonArray)
                {
                    Update(item);
                }
            }, out ExceptionStr);

        }

        public virtual void Delete(string KeyGuid)
        {
            @DBUtil.ExecuteSQL(string.Format("delete from {0} where {1}='{2}'", TableName, KeyName, KeyGuid));
        }

        public virtual void BatchDelete(List<string> KeyGuidList)
        {
            string ExceptionStr = string.Empty;
            @DBUtil.ExecuteTransition_SDB(delegate()
            {
                foreach (string KeyGuid in KeyGuidList)
                {
                    Delete(KeyGuid);
                }
            }, out ExceptionStr);
        }
    }
}
